<template>
  <el-form ref="dataForm" :rules="rules" :label-position="labelPosition" label-width="80px" :model="user">
    <el-form-item label="账号" prop="username">
      <el-input v-model="user.username" disabled type="text" placeholder="请输入账号" />
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="user.name" type="text" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="电话" prop="phone">
      <el-input v-model="user.phone" type="text" placeholder="请输入电话" />
    </el-form-item>
    <el-form-item label="邮件" prop="email">
      <el-input v-model="user.email" type="text" placeholder="请输入邮件" />
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="user.sex">
        <el-radio label="男">男</el-radio>
        <el-radio label="女">女</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getInfo } from '@/api/user'

export default {
  data() {
    return {
      labelPosition: 'right',
      user: {
        name: '',
        username: '',
        sex: '',
        phone: '',
        email: ''
      },
      rules: {
        name: [{ required: true, message: '账号必填', trigger: 'blur' }],
        phone: [{ required: true, message: '电话必填', trigger: 'blur' }],
        email: [{ required: true, message: '邮件必填', trigger: 'blur' }],
        sex: [{ required: true, message: '性别必填', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUser()
  },
  methods: {
    getUser() {
      getInfo().then(response => {
        const { data } = response
        const { name, username, avatar, sex, phone, email } = data.user
        this.user = {
          name: name,
          username: username,
          sex: sex,
          phone: phone,
          email: email,
          avatar: avatar
        }
      })
    }
  }
}
</script>
